package com.ormlite.activity.tool.button.movie;

import static com.ormlite.activity.tool.Utils.toDouble;
import static com.ormlite.activity.tool.Utils.toInteger;
import android.app.Activity;
import android.view.View;

import com.ormlite.activity.tool.DatabaseUtils;
import com.ormlite.activity.tool.GuiUtils;
import com.ormlite.activity.tool.Utils;
import com.ormlite.app.R;
import com.ormlite.pojo.Movie;

public class ModifyUpdateMovieButton implements View.OnClickListener {

	private final Activity activity;
	private final Integer id;

	public ModifyUpdateMovieButton(Activity activity, Integer id) {
		this.activity = activity;
		this.id = id;
	}

	@Override
	public void onClick(View v) {

		String title = GuiUtils.getEditText(activity, R.id.titleModify);
		String director = GuiUtils.getEditText(activity, R.id.directorModify);
		Integer year = toInteger(GuiUtils.getEditText(activity, R.id.yearModify));
		Double rate = toDouble(GuiUtils.getEditText(activity, R.id.rateModify));
		
		Movie movie = new Movie(title, year, director, rate);
		if (Utils.checkMovieFields(movie)) {
			GuiUtils.showAlertDialog(activity, "The only field which can be empty is 'rate', try again");
			return;
		}
		DatabaseUtils.updateMovieById(id, movie);

		activity.finish();
	}
}
